package petstore.persistence.user;

import petstore.persistence.DBUtil;
import petstore.persistence.user.User;
import petstore.persistence.user.UserDAO;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class UserDAOImpl implements UserDAO
{
    private static final String INSERT_USER = "INSERT INTO userinfo (username,password) VALUES (?,?)";
    private static final String WANTED_USER_ByUsername_And_Password = "SELECT * FROM userinfo WHERE username = ? AND password = ?";
    private static final String WANTED_USER_ByUser_id = "SELECT  * FROM userinfo WHERE user_id = ?";
    private static final String Find_All_User = "SELECT * FROM userinfo";
    private static final String WANTED_USER_ByUsername = "SELECT * FROM userinfo WHERE binary username = ?";
    private static final String UPDATA_USERDATA = "UPDATE userinfo SET key_cat = ?,key_dog = ?,key_bird = ?, key_pokemon = ?, key_bugCat = ? WHERE user_id = ?";

    //获取所有用户信息
    public List<User> Find_All_User()
    {
        List<User> userList = new ArrayList<>();
        try
        {
            Connection connection = DBUtil.getConnection();
            Statement statement = connection.createStatement();
            ResultSet resultSet = statement.executeQuery(Find_All_User);
            while(resultSet.next())
            {
                User user = this.ResultSetToUser(resultSet);    //获取当前行的用户属性
                userList.add(user);//添加到userlist中
            }
            DBUtil.closeResultSet(resultSet);
            DBUtil.closeStatement(statement);
            DBUtil.closeConnection(connection);
            //关闭
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return userList;

    }

    public User FindUserByUsernameAndPassword(String username,String password)
    {
        User result = null;
        try
        {
            Connection connection = DBUtil.getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(WANTED_USER_ByUsername_And_Password);
            preparedStatement.setString(1,username);
            preparedStatement.setString(2,password);
            ResultSet resultSet = preparedStatement.executeQuery();
            if(resultSet.next())
            {
               result = ResultSetToUser(resultSet);
            }
            DBUtil.closeResultSet(resultSet);
            DBUtil.closePreparedStatement(preparedStatement);
            DBUtil.closeConnection(connection);

        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return result;
    }

    public User FindUserByUser_id(int user_id)
    {
        User result = null;
        try
        {
            Connection connection = DBUtil.getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(WANTED_USER_ByUser_id);
            preparedStatement.setInt(1,user_id);
            ResultSet resultSet = preparedStatement.executeQuery();
            if(resultSet.next())
            {
                result = ResultSetToUser(resultSet);
            }
            DBUtil.closeResultSet(resultSet);
            DBUtil.closePreparedStatement(preparedStatement);
            DBUtil.closeConnection(connection);

        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public boolean UpdataUserdata(int user_id,String categoryId) {
        boolean success = false;
        try
        {
        //    System.out.println("cate::" + categoryId + " " + (categoryId.equals("cat")));
            Connection connection = DBUtil.getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(UPDATA_USERDATA);
            User user = FindUserByUser_id(user_id);
            if(categoryId.equals("cat")) {user.setKey_cat(user.getKey_cat() + 1);}
            if(categoryId.equals("dog")) user.setKey_dog(user.getKey_dog() + 1);
            if(categoryId.equals("bird")) user.setKey_bird(user.getKey_bird() + 1);
            if(categoryId.equals("pokemon")) user.setKey_pokemon(user.getKey_pokemon() + 1);
            if(categoryId.equals("bugCat")) user.setKey_bugcat(user.getKey_bugcat() + 1);
            preparedStatement.setDouble(1,user.getKey_cat());
            preparedStatement.setDouble(2,user.getKey_dog());
            preparedStatement.setDouble(3,user.getKey_bird());
            preparedStatement.setDouble(4,user.getKey_pokemon());
            preparedStatement.setDouble(5,user.getKey_bugcat());
            preparedStatement.setInt(6,user_id);
            int rows = preparedStatement.executeUpdate();
            if(rows == 1)
            {
                success = true;
                System.out.println("添加成功");
            }
            else
            {
                System.out.println("添加失败");
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return success;
    }

    //插入新用户
    public boolean InsertUser(User user)
    {
        boolean success = false;
        try
        {
            Connection connection = DBUtil.getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(INSERT_USER);
            preparedStatement.setString(1,user.getUsername());
            preparedStatement.setString(2,user.getPassword());
            int rows = preparedStatement.executeUpdate();
            if(rows == 1)
            {
                success = true;
                System.out.println("添加成功");
            }
            else
            {
                System.out.println("添加失败");
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return success;
    }

    private User ResultSetToUser(ResultSet resultSet) throws SQLException {
        User user = new User();
        user.setUser_id(resultSet.getInt("user_id"));
        user.setUsername(resultSet.getString("username"));
        user.setPassword(resultSet.getString("password"));
        user.setKey_bird(resultSet.getDouble("key_bird"));
        user.setKey_cat(resultSet.getDouble("key_cat"));
        user.setKey_dog(resultSet.getDouble("key_dog"));
        user.setKey_pokemon(resultSet.getDouble("key_pokemon"));
        user.setKey_bugcat(resultSet.getDouble("key_bugCat"));
        return user;
    }

    public boolean FindUserByUsername(String username){
        boolean result=false;
        try
        {
            Connection connection = DBUtil.getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(WANTED_USER_ByUsername);
            preparedStatement.setString(1,username);
            ResultSet resultSet = preparedStatement.executeQuery();
            if(resultSet.next())
            {
                result=true;
            }
            DBUtil.closeResultSet(resultSet);
            DBUtil.closePreparedStatement(preparedStatement);
            DBUtil.closeConnection(connection);

        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public boolean UpdataUserKey(int user_id) {

        boolean success = false;
        try
        {
            System.out.println(user_id);
            Connection connection = DBUtil.getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(UPDATA_USERDATA);
            User user = FindUserByUser_id(user_id);
            if(user == null) return false;
            preparedStatement.setDouble(1,user.getKey_cat() * 0.5);
            preparedStatement.setDouble(2,user.getKey_dog() * 0.5);
            preparedStatement.setDouble(3,user.getKey_bird() * 0.5);
            preparedStatement.setDouble(4,user.getKey_pokemon() * 0.5);
            preparedStatement.setDouble(5,user.getKey_bugcat() * 0.5);
            preparedStatement.setInt(6,user_id);
            int rows = preparedStatement.executeUpdate();

            if(rows == 1)
            {
                success = true;
                System.out.println("添加成功");
            }
            else
            {
                System.out.println("添加失败");
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return success;
    }
}
